package com.ninth_group.po;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.MonthDaySerializer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;

public class Forecast7D {
    private String cid;
    // 指定日期格式, 注意要重写toString, 否则打印的时间以`--MM/dd`格式输出
    @JsonSerialize(using = MonthDaySerializer.class)
    @JsonDeserialize(using = MonthDayDeserializer.class)
    @JsonFormat(pattern = "MM/dd")
    private MonthDay forecastDate;
    private String weather;
    private int tempMax;
    private int tempMin;
    private String windDirection;
    private int windScale;
    private String uv;
    private String aq;

    // 定义日期格式
    private static final DateTimeFormatter PARSE_FORMATTER =
            DateTimeFormatter.ofPattern("MM-dd");

    // 映射结果集
    public static Forecast7D mapRow(ResultSet rs) throws SQLException {
        Forecast7D f7d = new Forecast7D();
        f7d.setCid(rs.getString("city_id"));
        f7d.setForecastDate(MonthDay.parse(
                rs.getString("forecast_date"), PARSE_FORMATTER));
        // f7d.setForecastDate(MonthDay.parse(rs.getString("forecast_date"))); 不能直接解析, 要指定日期格式
        f7d.setWeather(rs.getString("weather"));
        f7d.setTempMax((int) Double.parseDouble(rs.getString("temp_max")));
        f7d.setTempMin((int) Double.parseDouble(rs.getString("temp_min")));
        f7d.setWindDirection(rs.getString("wind_direction"));
        f7d.setWindScale(rs.getInt("wind_scale")); // hive表中就是int类型
        f7d.setUv(rs.getString("uv"));
        f7d.setAq(rs.getString("aq"));
        return f7d;
    }

    public Forecast7D() {
    }
    public Forecast7D(String cid, MonthDay forecastDate, String weather,
                      int tempMax, int tempMin, String windDirection,
                      int windScale, String uv, String aq) {
        this.cid = cid;
        this.forecastDate = forecastDate;
        this.weather = weather;
        this.tempMax = tempMax;
        this.tempMin = tempMin;
        this.windDirection = windDirection;
        this.windScale = windScale;
        this.uv = uv;
        this.aq = aq;
    }

    @Override
    public String toString() {
        return "Forecast7D{" +
                "cid='" + cid + '\'' +
                ", forecastDate=" + forecastDate +
                ", weather='" + weather + '\'' +
                ", tempMax=" + tempMax +
                ", tempMin=" + tempMin +
                ", windDirection='" + windDirection + '\'' +
                ", windScale=" + windScale +
                ", uv='" + uv + '\'' +
                ", aq='" + aq + '\'' +
                '}';
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public MonthDay getForecastDate() {
        return forecastDate;
    }

    public void setForecastDate(MonthDay forecastDate) {
        this.forecastDate = forecastDate;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public int getTempMax() {
        return tempMax;
    }

    public void setTempMax(int tempMax) {
        this.tempMax = tempMax;
    }

    public int getTempMin() {
        return tempMin;
    }

    public void setTempMin(int tempMin) {
        this.tempMin = tempMin;
    }

    public String getWindDirection() {
        return windDirection;
    }

    public void setWindDirection(String windDirection) {
        this.windDirection = windDirection;
    }

    public int getWindScale() {
        return windScale;
    }

    public void setWindScale(int windScale) {
        this.windScale = windScale;
    }

    public String getUv() {
        return uv;
    }

    public void setUv(String uv) {
        this.uv = uv;
    }

    public String getAq() {
        return aq;
    }

    public void setAq(String aq) {
        this.aq = aq;
    }
}
